package com.xindz.hgq.database.greendao;

import android.support.annotation.WorkerThread;

import com.alibaba.fastjson.JSONObject;
import com.blankj.utilcode.constant.TimeConstants;
import com.blankj.utilcode.util.TimeUtils;
import com.orhanobut.logger.Logger;
import com.xindz.hgq.database.greendao.bean.DaoSession;
import com.xindz.hgq.database.greendao.bean.DataCacheBean;
import com.xindz.hgq.database.greendao.bean.DataCacheBeanDao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @name HGQBase
 * @class name：com.xindz.hgq.database.greendao
 * @describe describe
 * @anthor XINDZ
 * @time 2018/5/16 9:54
 * @change
 * @chang time
 * @class describe
 */
public class GdDataCacheManager {
    private static volatile GdDataCacheManager manage;
    private DataCacheBeanDao dataCacheBeanDao;
    private DaoSession daoSession;

    private GdDataCacheManager() {
    }


    public static GdDataCacheManager getInstance(DaoSession daoSession) {
        if (null == manage) {
            synchronized (GdDataCacheManager.class) {
                if (null == manage) {
                    manage = new GdDataCacheManager();
                }
            }
        }
        manage.daoSession = daoSession;
        manage.dataCacheBeanDao = daoSession.getDataCacheBeanDao();
        return manage;
    }

    /**
     * 缓存数据
     *
     * @param key        网络请求唯一标识 主键
     * @param jsonString 实体转化的jsonString
     * @return 是否保存成功
     */
    public boolean saveDataCache(String key, String jsonString) {

        boolean flag = false;
        if (null == key || "".equals(key) || null == jsonString || "".equals(jsonString)) {
            return false;
        }
        List<String> dataList = new ArrayList<>();
        dataList.add(jsonString);
        flag = saveDataCache(key, dataList);

        return flag;
    }

    /**
     * 缓存数据
     *
     * @param key            网络请求唯一标识 主键
     * @param jsonStringList 实体转化的jsonString
     * @return 是否保存成功
     */
    public boolean saveDataCache(String key, List<String> jsonStringList) {
        boolean flag = false;
        if (null == key || "".equals(key) || null == jsonStringList || jsonStringList.size() <= 0) {
            return false;
        }

        try {
            Date dateByNow = TimeUtils.getDateByNow(System.currentTimeMillis(), TimeConstants.MSEC);
            DataCacheBean dataCacheBean = new DataCacheBean(key, jsonStringList, dateByNow);
            flag = dataCacheBeanDao.insertOrReplace(dataCacheBean) != -1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 根据Key读取缓存
     *
     * @param key 缓存存储的唯一标识
     * @param c   缓存的数据的类型的 class
     * @param <T> 缓存的数据的类型
     * @return 缓存数据数组 null 表示没有查到缓存数据
     */
    public <T> ArrayList<T> queryDataCache(final String key, final Class<T> c) {
        ArrayList<T> dataList = new ArrayList<>();
        if (null == key || "".equals(key) || null == c) {
            return null;
        }
        try {
            List<DataCacheBean> list = dataCacheBeanDao.queryBuilder().where(DataCacheBeanDao.Properties.Key.eq(key)).list();
            if (list != null && list.size() > 0) {
                for (DataCacheBean dataCacheBean : list) {
                    List<String> cacheDataList = dataCacheBean.getCacheData();
                    if (null != cacheDataList) {
                        for (String cacheData : cacheDataList) {
                            if (cacheData != null && !"".equals(cacheData) && cacheData.length() > 0) {
                                Logger.i("cacheData:" + cacheData);
                                T d = JSONObject.parseObject(cacheData, c);
                                if (d != null) {
                                    dataList.add(d);
                                }
                            }
                        }
                    }
                }
            } else {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return dataList;
    }

    /**
     * 根据Key删除缓存
     *
     * @param key 缓存存储的唯一标识
     * @return 是否成功执行删除操作
     */
    public boolean deleteByKey(final String key) {
        boolean flag = false;
        if (null == key || "".equals(key)) {
            return false;
        }
        try {
            daoSession.runInTx(new Runnable() {
                @Override
                public void run() {
                    dataCacheBeanDao.deleteByKey(key);
                }
            });
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 清除所有数据缓存(好使操作需要放在子线程中进行操作)
     *
     * @return 是否成功执行清除操作
     */
    public boolean clearAllDataCache() {
        boolean flag = false;
        try {
            daoSession.runInTx(new Runnable() {
                @Override
                public void run() {
                    dataCacheBeanDao.deleteAll();
                }
            });
            flag = true;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }
}
